package com.atguigu.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.listen.DictListen;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织架构表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-11-03
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Cacheable(value = "dict")
    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        for (Dict dict : dictList) {
            Long dictId = dict.getId();
            boolean children = this.isChildren(dictId);
            dict.setHasChildren(children);
        }
        return dictList;
    }

    private boolean isChildren(Long id){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        return dictList.size() > 0;
    }

    @Override
    public void downLoad(HttpServletResponse response) throws IOException {
        List<Dict> dictList = baseMapper.selectList(null);
        List<DictEeVo> dictEeVoList = new ArrayList<>();
        for (Dict dict : dictList) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }

        EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictEeVoList);
    }

    @CacheEvict(value = "dict",allEntries = true)
    @Override
    public void upLoad(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListen(baseMapper)).sheet().doRead();
    }

    @Override
    public String getName(String parentDictCode, Long value) {
        if (StringUtils.isEmpty(parentDictCode)){
            //查询地址
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(queryWrapper);
            return dict.getName();
        }else {
            //查询医院类型
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_code",parentDictCode);
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>().
                    eq("parent_id", baseMapper.selectOne(queryWrapper).getId()).
                    eq("value", value));
            return dict.getName();
        }
    }

    @Override
    public List<Dict> findProvince(String province) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",province);
        List<Dict> dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", baseMapper.selectOne(queryWrapper).getId()));
        return dictList;
    }

    @Override
    public List<Dict> findCity(Long parentId) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        return dictList;
    }

    @Override
    public List<Map<String,String>> findByDictCode(String dictCode) {
        List<Map<String,String>> list = new ArrayList<>();
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",dictCode);
        List<Dict> dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", baseMapper.selectOne(queryWrapper).getId()));
        for (Dict dict : dictList) {
            Map<String,String> map = new HashMap<>();
            map.put("name",dict.getName());
            map.put("value",dict.getValue());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<DictEeVo> findChildDataByParentId(Long parentId) {
        List<DictEeVo> list = new ArrayList<>();
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        for (Dict dict : dictList) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            list.add(dictEeVo);
        }
        return list;
    }
}
